package com.hp.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.entity.*;
import com.hp.service.UserService;
import com.hp.util.GeneratID;
import com.hp.util.Logblog;
import com.hp.util.ResultJson;
import com.hp.util.Utils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping("/login")
    //登录
    public void login(User user, String checkCode, HttpServletResponse response, HttpServletRequest request) {
        User user1 = userService.login(user);
        request.getSession().setAttribute("email",user1.getEmail());
        request.getSession().setAttribute("id",user1.getId());
        String code = (String) request.getSession().getAttribute("code");
        if (user1 != null && code.equals(checkCode)) {
            LoginInfor loginInfor =new LoginInfor();
            String description= (String) request.getSession().getAttribute("email");
            loginInfor.setDescription(description);
            Date data = new Date();
            loginInfor.setCreate_datetime(data);
            //int i=loginInforService.add(loginInfor);
            request.getSession().setAttribute("user1", user1);
            ResultJson success = ResultJson.success(user1);
            Utils.returnJson(success, response);
        } else {
            ResultJson error = ResultJson.error();
            Utils.returnJson(error, response);
        }
    }

    //权限登录
    @RequestMapping("/getPermissions")
    public void getPermissions(HttpServletResponse response, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user1");
        Integer id = user.getId();
        List<Permissions> list = userService.getPermissions(id);
        Utils.returnJson(ResultJson.success(list),response);
    }
    //获取所有角色
    @RequestMapping("/getAllRole")
    public void getAllRole(HttpServletResponse response, HttpServletRequest request) {
        List<Role> list = userService.getAllRole();
        Utils.returnJson(ResultJson.success(list),response);
    }
    /**
     * 获取验证码
     */
    @RequestMapping("/checkCode")
    public void checkCode(HttpSession session, HttpServletResponse response) throws IOException {
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 5);
        String code = circleCaptcha.getCode();
        session.setAttribute("code", code);
        circleCaptcha.write(response.getOutputStream());
    }

    @RequestMapping("/findAll")
    public void findAll(User user, @RequestParam(value = "page",defaultValue = "1")Integer page,
                        @RequestParam(value = "limit",defaultValue = "10")Integer limit, HttpServletResponse response){
        System.out.println(user);
        PageHelper.startPage(page,limit);
        List<User> list = userService.findAll(user,page,limit);
        PageInfo<User> userPageInfo = new PageInfo<User>(list);
        if (list.size() != 0) {
            Utils.returnJson(ResultJson.success(userPageInfo), response);
        }else {
            Utils.returnJson(ResultJson.error(),response);
        }
    }
    @RequestMapping("/findById")
    public void findById(User user, HttpServletResponse response){
        System.out.println(user);
        User list = userService.findById(user);
        if (list != null) {
            Utils.returnJson(ResultJson.success(list), response);
        }else {
            Utils.returnJson(ResultJson.error(),response);
        }
    }
    @RequestMapping("/add")
    public void add(Patient patient, HttpServletResponse response){
        int i = userService.add(patient);
        if (i > 0) {
            Utils.returnJson(ResultJson.success(i), response);
        }else {
            Utils.returnJson(ResultJson.error(),response);
        }
    }
    @RequestMapping("/delete")
    public void delete(User user, HttpServletResponse response){
        System.out.println(user);
        int i = userService.delete(user);
        if (i > 0) {
            User user1 = userService.findById(user);
            Utils.returnJson(ResultJson.success(user1), response);
        }else {
            Utils.returnJson(ResultJson.error(505,"无法挂号了！！"),response);
        }
    }
    //登陆记录查询
    @RequestMapping("/selectAll")
    public void select(HttpServletRequest request, HttpServletResponse response, @Param("limit") Integer limit, @Param("page") Integer page){
        String email= (String) request.getSession().getAttribute("email");
        List<User> list=userService.selectAll(email,limit,page);
        PageInfo<User> pageInfo=new PageInfo<>(list);
        ResultJson success = ResultJson.success(pageInfo);
        Utils.returnJson(success,response);
    }
    //
    @RequestMapping("/update")
    public void update(HttpServletRequest request, HttpServletResponse response,User user){
        String email= (String) request.getSession().getAttribute("email");
        user.setEmail(email);
        int i=userService.update(user);
        System.out.println(i);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success,response);
    }
    //修改密码
    @RequestMapping("/findAlle")
    public void findAll(HttpServletRequest request, HttpServletResponse response,String email){
        email= (String) request.getSession().getAttribute("email");
        List<User> hisUser = userService.findAlle(email);
        ResultJson success = ResultJson.success(hisUser);
        Utils.returnJson(success,response);
    }
    //个人中心个人资料查询
    @RequestMapping("/findAlltow")
    public void findAlltow(HttpServletRequest request, HttpServletResponse response,String email){
        email= (String) request.getSession().getAttribute("email");
        User hisUser = userService.findAlltow(email);
        ResultJson success = ResultJson.success(hisUser);
        Utils.returnJson(success,response);
    }
    //修改个人中心个人资料
    @RequestMapping("/updatetow")
    public void updatetow(HttpServletRequest request, HttpServletResponse response,User user){
        String email = (String) request.getSession().getAttribute("email");
        user.setEmail(email);
        int i=userService.updatetow(user);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success,response);
    }
    //查询已拥有角色
    @RequestMapping("/addfind")
    public void addfind(HttpServletRequest request, HttpServletResponse response){
        String email = (String) request.getSession().getAttribute("email");
        List<User> hisUser=userService.addfind(email);
        System.out.println(hisUser);
        ResultJson success = ResultJson.success(hisUser);
        Utils.returnJson(success,response);
    }
    //添加角色查询下拉列表
    @RequestMapping("/selectrole")
    public void selectrole( HttpServletResponse response){
        List<Role> hisRole=userService.selectrole();
        System.out.println("----------"+hisRole);
        ResultJson success = ResultJson.success(hisRole);
        Utils.returnJson(success,response);
    }

    //添加角色
    @RequestMapping("/add1")
    public void add(HttpServletRequest request, HttpServletResponse response, UserRole userRole){
        Integer uid = (int) request.getSession().getAttribute("id");
        userRole.setUid(uid);
        Date data = new Date();
        userRole.setCreate_datetime(data);
        int i=userService.add1(userRole);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success,response);
    }
}
